package dataflowScheme.diagram.customs;

import org.eclipse.draw2d.AbstractConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * intended to set custom anchor for connecting a wire to the port.
 * 
 * @author <a href="mailto:misiak7@gmail.com">Michal Antolik</a>
 *
 */
public class PortConnectionAnchor extends AbstractConnectionAnchor {

	public PortConnectionAnchor(IFigure owner) {
		super(owner);
	}
	
	
	public Point getLocation(Point reference) {
		Rectangle r = getOwner().getBounds();
		
		String orientation = null;
		
		for(Object o : getOwner().getChildren()){
			if(o instanceof ConnectionTypeFigure){
				orientation = ((ConnectionTypeFigure)o).getConnectionType();
				break;
			}
		}
		
		int x,y;
		x = r.x;
		y = r.y + r.height/2;
		
		if((orientation != null) && (orientation.equals("Out"))){
			x += r.width;
		}
		
		Point p = new PrecisionPoint(x,y);
		getOwner().translateToAbsolute(p);
		return p;
	}
	
	public Point getReferencePoint(){
		return getLocation(null);
	}
		

}
